#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
    int dx[4] = { 1,-1,0,0 };
    int dy[4] = { 0,0,-1,1 };
    int oldcolor;
    int m, n;
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
        oldcolor = image[sr][sc];
        m = image.size();
        n = image[0].size();
        if (color == oldcolor) return image;
        dfs(image, sr, sc, color);
        return image;
    }
    void dfs(vector<vector<int>>& image, int sr, int sc, int color)
    {
        image[sr][sc] = color;
        for (int i = 0;i < 4;i++)
        {
            int x = sr + dx[i], y = sc + dy[i];
            if (x >= 0 && x < m && y >= 0 && y < n && image[x][y] == oldcolor)
            {
                dfs(image, x, y, color);
            }
        }
    }
};